package com.projects.android.bus;

import java.net.URI;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONObject;

import android.util.Log;

import com.projects.android.dto.DistrictDTO;
import com.projects.android.utils.Utils;

public class DistrictBUS {
	public static ArrayList<DistrictDTO> selectByIDCity(int id) {
		ArrayList<DistrictDTO> myData = null;
		try {
			Map<String, String> mapUser = new HashMap<String, String>();
			mapUser.put("idcity", String.valueOf(id));
			String url = Utils.buildURL(Utils.SERVICE_URI
					+ "/District_SelectByIDCity", mapUser);
			
			// Send GET request to <service>/GetPlates
			HttpClient httpClient = new DefaultHttpClient();
			HttpGet request = new HttpGet(new URI(url));
			request.setHeader("Accept", "application/json");
			request.setHeader("Content-type", "application/json");

			HttpResponse response = httpClient.execute(request);
			HttpEntity responseEntity = response.getEntity();
			// Read response data into buffer
			String result = Utils.getResponse(responseEntity);
			Log.i("DistrictBUS selectByIDCity", "Response: " + result);

			if (result != "") {
				// Init array objects
				JSONArray arrData = new JSONArray(result);
				myData = new ArrayList<DistrictDTO>();
				// Loop for all reveived objects
				for (int i = 0; i < arrData.length(); i++) {
					JSONObject data = (JSONObject) arrData.get(i);
					DistrictDTO dto = new DistrictDTO();
					dto.initObject(data);
					myData.add(dto);
				}
			}
		} catch (Exception e) {
			Log.e("CityBUS loadAllData", e.toString());
		}
		return myData;
	}
}
